package cn.trip.biz.service.impl;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripAreaDic;
import cn.itrip.beans.pojo.ItripHotel;
import cn.itrip.beans.pojo.ItripHotelWithBLOBs;
import cn.itrip.beans.pojo.ItripLabelDic;
import cn.itrip.beans.vo.ItripImageVO;
import cn.itrip.common.RedisUtil;
import cn.itrip.dao.ItripAreaDicMapper;
import cn.itrip.dao.ItripHotelMapper;
import cn.itrip.dao.ItripImageMapper;
import cn.itrip.dao.ItripLabelDicMapper;
import cn.trip.biz.service.ItripHotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("itripHotelService")
public class ItripHotelServiceImpl implements ItripHotelService {
    @Autowired
    private ItripHotelMapper itripHotelMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ItripImageMapper itripImageMapper;
    @Autowired
    private ItripAreaDicMapper itripAreaDicMapper;
    @Autowired
    private ItripLabelDicMapper itripLabelDicMapper;

    /**
     * redisKey hotelDetail:hotelId=+hotelId
     * 时间10分钟
     *
     * @param id
     * @return
     */
    @Override
    public Dto<Map<String, Object>> selectHotelByid(Long id) {
        String key = "hotelDetail:hotelId=" + id;
        ItripHotelWithBLOBs itripHotelWithBLOBs = null;
        Dto<Map<String, Object>> dto = new Dto<>();
        if (redisUtil.hasKey(key)) {
            itripHotelWithBLOBs = (ItripHotelWithBLOBs) redisUtil.get(key);
        } else {
            itripHotelWithBLOBs = itripHotelMapper.selectByPrimaryKey(id);
        }
        if (itripHotelWithBLOBs != null) {
            redisUtil.set(key, itripHotelWithBLOBs, 600);
            Map<String, Object> itripHotelDescVO = new HashMap<>();
            itripHotelDescVO.put("hotelId", itripHotelWithBLOBs.getId());
            itripHotelDescVO.put("hotelName", itripHotelWithBLOBs.getHotelName());
            itripHotelDescVO.put("hotelLevel", itripHotelWithBLOBs.getHotelLevel());
            dto.setSuccess("true");
            dto.setErrorCode("0");
            dto.setData(itripHotelDescVO);

        } else {
            dto.setSuccess("false");
            dto.setMsg("没有酒店信息");
        }
        return dto;
    }

    /**
     * redisKey ItripImage:targetId
     * 时间10分钟
     *
     * @return
     */
    @Override
    public Dto<List<ItripImageVO>> getItripImage(Map<String, Object> param) {
        String key = "ItripImage:" + param.get("targetId");
        List<ItripImageVO> itripImageVOList = null;
        Dto<List<ItripImageVO>> dto = new Dto<>();
        if (redisUtil.hasKey(key)) {
            itripImageVOList = (List<ItripImageVO>) redisUtil.get(key);
        } else {
            itripImageVOList = itripImageMapper.getItripImage(param);
        }

        if (itripImageVOList != null && itripImageVOList.size() > 0) {
            redisUtil.set(key, itripImageVOList, 600);
            dto.setSuccess("true");
            dto.setErrorCode("0");
            dto.setData(itripImageVOList);
        } else {
            dto.setSuccess("true");
            dto.setMsg("没有评价详情");
        }
        return dto;
    }

    /**
     * redisKey hotelDetail:hotelId
     * redisKey hotelFeature:hotelId
     * 时间10分钟
     *
     * @param hotelId
     * @return
     */
    @Override
    public Dto getHotelDetails(Long hotelId) {
        Dto dto = new Dto();
        List list = new ArrayList();
        String key = "hotelDetail:hotelId=" + hotelId;
        String key2 = "hotelFeature:hotelId=" + hotelId;

        ItripHotelWithBLOBs itripHotelWithBLOBs;
        if (redisUtil.hasKey(key)) {
            itripHotelWithBLOBs = (ItripHotelWithBLOBs) redisUtil.get(key);
        } else {
            itripHotelWithBLOBs = itripHotelMapper.selectByPrimaryKey(hotelId);
        }
        Map<String, Object> map1 = new HashMap<>();
        if (itripHotelWithBLOBs != null) {
            redisUtil.set(key, itripHotelWithBLOBs, 600);
            map1.put("name", "酒店介绍");
            map1.put("description", itripHotelWithBLOBs.getDetails());
        }
        list.add(map1);


        List<ItripLabelDic> itripLabelDicList;
        if (redisUtil.hasKey(key2)) {
            itripLabelDicList = (List<ItripLabelDic>) redisUtil.get(key2);
        } else {
            itripLabelDicList = itripLabelDicMapper.getHotelFeatureByHotelId(hotelId);
        }
        if (itripLabelDicList != null) {
            redisUtil.set(key2, itripLabelDicList, 600);
            for (ItripLabelDic label : itripLabelDicList) {
                Map<String, Object> map = new HashMap();
                map.put("name", label.getName());
                map.put("description", label.getDescription());
                list.add(map);
            }
        }
        dto.setSuccess("true");
        dto.setErrorCode("0");
        dto.setData(list);
        return dto;
    }

    /**
     * redisKey hotelDetail:hotelId
     * 时间10分钟
     *
     * @param hotelId
     * @return
     */
    @Override
    public Dto getHotelFaclities(Long hotelId) {
        Dto dto = new Dto();
        String key = "hotelDetail:hotelId=" + hotelId;
        ItripHotelWithBLOBs itripHotelWithBLOBs;
        if (redisUtil.hasKey(key)) {
            itripHotelWithBLOBs = (ItripHotelWithBLOBs) redisUtil.get(key);
        } else {
            itripHotelWithBLOBs = itripHotelMapper.selectByPrimaryKey(hotelId);
        }
        if (itripHotelWithBLOBs != null) {
            redisUtil.set(key, itripHotelWithBLOBs, 600);
            dto.setData(itripHotelWithBLOBs.getFacilities());
        }
        dto.setSuccess("true");
        dto.setErrorCode("0");
        return dto;
    }

    /**
     * redisKey hotelDetail:hotelId
     * 时间10分钟
     *
     * @param hotelId
     * @return
     */
    @Override
    public Dto getHotelPolicy(Long hotelId) {
        Dto dto = new Dto();
        String key = "hotelDetail:hotelId=" + hotelId;
        ItripHotelWithBLOBs itripHotelWithBLOBs;
        if (redisUtil.hasKey(key)) {
            itripHotelWithBLOBs = (ItripHotelWithBLOBs) redisUtil.get(key);
        } else {
            itripHotelWithBLOBs = itripHotelMapper.selectByPrimaryKey(hotelId);
        }
        if (itripHotelWithBLOBs != null) {
            redisUtil.set(key, itripHotelWithBLOBs, 600);
            dto.setData(itripHotelWithBLOBs.getHotelPolicy());
        }
        dto.setSuccess("true");
        dto.setErrorCode("0");
        return dto;
    }

    /**
     * redisKey (商圈) areaDicName:hotelId=hotelId
     * redisKey (特色) hotelFeature:hotelId=hotelId
     * redisKey (酒店详情) hotelDetail:hotelId
     * 时间10分钟
     *
     * @param hotelId
     * @return
     */
    @Override
    public Dto getVideoDesc(Long hotelId) {
        Dto dto = new Dto();
        Map<String, Object> map = new HashMap<>();

        String key = "areaDicName:hotelId=" + hotelId;
        //商圈
        List<ItripAreaDic> itripAreaDic;
        if (redisUtil.hasKey(key)) {
            itripAreaDic = (List<ItripAreaDic>) redisUtil.get(key);
        } else {
            itripAreaDic = itripAreaDicMapper.getAreaNameByHotelId(hotelId);
        }
        if (itripAreaDic != null) {
            redisUtil.set(key, itripAreaDic, 600);
            List<String> hotelLabelDic = new ArrayList<>();
            for (ItripAreaDic itripAreaDic1 : itripAreaDic) {
                hotelLabelDic.add(itripAreaDic1.getName());
            }
            map.put("tradingAreaNameList", hotelLabelDic);
        }

        String key2 = "hotelFeature:hotelId=" + hotelId;
        //特色
        List<ItripLabelDic> itripLabelDic;
        if (redisUtil.hasKey(key2)) {
            itripLabelDic = (List<ItripLabelDic>) redisUtil.get(key2);
        } else {
            itripLabelDic = itripLabelDicMapper.getHotelFeatureByHotelId(hotelId);
        }
        if (itripAreaDic != null) {
            redisUtil.set(key2, itripLabelDic, 600);
            List<String> hotelFeatureList = new ArrayList<>();
            for (ItripLabelDic itripLabelDic1 : itripLabelDic) {
                hotelFeatureList.add(itripLabelDic1.getName());
            }
            map.put("hotelFeatureList", hotelFeatureList);
        }

        String key3 = "hotelDetail:hotelId=" + hotelId;
        //酒店名
        ItripHotelWithBLOBs itripHotelWithBLOBs = null;
        if (redisUtil.hasKey(key3)) {
            itripHotelWithBLOBs = (ItripHotelWithBLOBs) redisUtil.get(key3);
        } else {
            itripHotelWithBLOBs = itripHotelMapper.selectByPrimaryKey(hotelId);
        }
        if (itripHotelWithBLOBs != null) {
            redisUtil.set(key3, itripHotelWithBLOBs, 600);
            map.put("hotelName", itripHotelWithBLOBs.getHotelName());
        }

        dto.setSuccess("true");
        dto.setErrorCode("0");
        dto.setData(map);
        return dto;
    }

    @Override
    public Dto getTradeArea(Long parent, Long isTradingArea) {
        Dto dto = new Dto();
        List area = new ArrayList<>();

        String key = "treadeAreaDic:parent=" + parent;
        List<ItripAreaDic> list;
        if (redisUtil.hasKey(key)) {
            list = (List<ItripAreaDic>) redisUtil.get(key);
        } else {
            list = itripAreaDicMapper.getTreadeAreaByParent(parent, isTradingArea);
        }
        if (list != null && list.size() > 0) {
            redisUtil.set(key, list, 600);
            for (ItripAreaDic itripAreaDic : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", itripAreaDic.getId());
                map.put("name", itripAreaDic.getName());
                area.add(map);
            }
        }
        dto.setSuccess("true");
        dto.setErrorCode("0");
        dto.setData(area);
        return dto;
    }


}
